/*
 * @Author: tangyuhui 317972442@qq.com
 * @Date: 2024-08-26 14:36:05
 * @LastEditors: tangyuhui 317972442@qq.com
 * @LastEditTime: 2024-09-14 22:04:53
 */
import React, { useState, useEffect } from "react";
import "./taskTable.less";
import Board from './Board';

const Dashboard = () => {
  const [elementsData, setElementsData] = useState([]);
  const [isExtension, setIsExtension] = useState(
    typeof chrome !== "undefined" && chrome.runtime.id
  );
  useEffect(() => {
    chrome.storage.local.get(["savedData"], function(result) {
      setElementsData(result.savedData)
    });
    fetchData()
  }, []); 
  const fetchData = async () => {
    if (isExtension) {
      console.log('点击了刷新数据')
      chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
        let message = { action: "getElements" };
        // 向content scripts发送消息
        if (tabs.length > 0 && tabs[0].id !== undefined) {
          chrome.tabs.sendMessage(tabs[0].id, message, (res) => {
            setElementsData(res?.data);
          });
        }
      });
    } else {
      // 普通页面情况下的处理
      console.log("Not running in Chrome extension environment.");
    }
  };
  return (
    <div className="pd-10" style={{ height: "600px", width: "800px",overflow: "auto" }}>
       <Board elementsData={elementsData} getElements={fetchData}></Board>
    </div>
  );
};



export default Dashboard;
